package com.sunyu.log.controller;


import com.alibaba.fastjson.JSON;
import com.boco.common.model.CommonResult;
import com.github.pagehelper.PageInfo;
import com.sunyu.log.model.Announcement;
import com.sunyu.log.service.AnnouncementService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author yu
 * @date 2017-05-08 17:11:50
 *
 */
@Controller
@RequestMapping("announcement")
public class AnnouncementController{
    /**
     * 日志
     */
    private static Logger logger = LoggerFactory.getLogger(AnnouncementController.class);

	@Resource
	private AnnouncementService announcementService;

	@ResponseBody
	@RequestMapping(value="/add",method = RequestMethod.POST)
	public CommonResult save(Announcement entity, HttpServletRequest request){
		// 从登陆中获取用户信息，设为发布人
		entity.setInsertOperator("admin");
		return announcementService.save(entity);
	}

	@ResponseBody
	@RequestMapping (value="/update",method = RequestMethod.GET)
	public CommonResult update(Announcement entity){
		return announcementService.update(entity);
	}

	@ResponseBody
	@RequestMapping(value="/delete/{id}")
	public CommonResult delete(@PathVariable int id){
		return announcementService.delete(id);
	}

	@ResponseBody
	@RequestMapping(value="/query/{id}")
	public CommonResult queryById(@PathVariable int id){
		return announcementService.queryById(id);
	}

    @ResponseBody
    @RequestMapping(value = "page/{offset}/{limit}")
    public PageInfo queryPage(@PathVariable int offset,@PathVariable int limit){
        return announcementService.queryPage(offset,limit);
    }

	/**
	 * 跳转到公告页面
	 * @author lidan
	 * */
	@RequestMapping(value="/toAnnouncement", method = RequestMethod.GET)
	public String toAnnouncement(){
		return "/announcement";
	}

	/**
	 * 跳转到更新公告页面
	 * @author lidan
	 * */
	@RequestMapping(value="/toUpdateAnnouncement/{id}", method = RequestMethod.GET)
	public String toUpdateAnnouncement(@PathVariable int id, Model model){
		CommonResult result = announcementService.queryById(id);
		System.out.println("result:"+ JSON.toJSONString(result));
		if(result.isSuccess()){
			Announcement announcement = (Announcement)result.getData();
			model.addAttribute("id", announcement.getId());
			model.addAttribute("title", announcement.getTitle());
			model.addAttribute("content", announcement.getContent());
			model.addAttribute("insertDatetime", announcement.getInsertDatetime());
			model.addAttribute("expiryDatetime", announcement.getExpiryDatetime());
			model.addAttribute("insertOperator", announcement.getInsertOperator());
		}
		return "/updateAnnouncement";
	}

	/**
	 * 跳转到公告列表页面
	 * @author lidan
	 * */
	@RequestMapping(value="/toAnnouncementList", method = RequestMethod.GET)
	public String toAnnouncementList(){
		return "/announcementList";
	}

	/**
	 * @param offset
	 * @param limit
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/page", method = RequestMethod.GET)
	public PageInfo page(int offset, int limit) {
		PageInfo<Announcement> result = announcementService.queryPage(offset, limit);
		return result;
	}

	/**
	 * 公告详情
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/article-{id}.htm",method = RequestMethod.GET)
	public  String toView(@PathVariable int id,Model model){
		CommonResult result = announcementService.queryById(id);
		if(result.isSuccess()){
			model.addAttribute("data",result.getData());
			return "forward:/views/detail.jsp";
		}else{
			//没有记录则返回一个错误页面，服务器自动处理为404
			return "forward:/xx.jsp";
		}
	}
}